package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.Global.Global;
import com.database.DBSystem;
import com.example.R;
import com.object.obDetailMau;
import com.object.obKho;
import com.object.obMau;
import com.object.obProductCategory;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class TK_ChonMau extends Activity {

	private Spinner spMau, spNhom, spMauDetail;
	private DatePicker dpNgay;
	private Button btXem;
	private EditText etChonKho;
	private LinearLayout llNgay, llNhom, llKho;
	private TextView textViewDetail;
	ArrayList<obProductCategory> pCategorys;
	ArrayList<obKho> Khos;
	ArrayList<obMau> maus;
	ArrayList<obDetailMau> detailMaus;
	private int REQUESTCODE_TONGHOP = 100;
	private int REQUESTCODE_CTTHU = 101;
	private int REQUESTCODE_CTCHI = 102;
	private int REQUESTCODE_CTNHAP = 103;
	private int REQUESTCODE_CTXUAT = 104;
	private int REQUESTCODE_TONGMATHANG = 105;
	private int REQUESTCODE_CUOCGOI = 106;
	private int REQUESTCODE_CHONKHO = 107;
	private int REQUESTCODE_NUOCVO = 108;
	private int REQUESTCODE_TKCUOCGOI = 109;
	private int REQUESTCODE_TK_DSNV = 110;
	private int REQUESTCODE_TK_XUATTON = 111;
	private int REQUESTCODE_CONGNO = 112;

	public static int CHOOSE_NHAP = 200;
	public static int CHOOSE_XUAT = 201;
	public static int CHOOSE_DSCUOCGOI = 202;
	public static int CHOOSE_CHUAGIAO = 203;
	public static int CHOOSE_CHUATHANHTOAN = 204;
	public static int CHOOSE_CHUAGIAO_CHUATHANTOAN = 205;
	public static int CHOOSE_CUOCGOI_DABAN = 206;
	public static int CHOOSE_CUOCGOI_CHUABAN = 207;
	public static int CHOOSE_THU = 208;
	public static int CHOOSE_CHI = 209;
	public static int CHOOSE_XNUOC_NVO = 300;
	public static int CHOOSE_XVO_NNUOC = 301;
	public static int CHOOSE_TONNV = 303;
	public static int CHOOSE_TONGHOP = 210;
	public static int CHOOSE_TONGMATHANG = 211;

	public static int CHOOSE_TKCUOCGOI = 400;
	public static int CHOOSE_TKCUOCGOIBYKHO = 401;

	public static int CHOOSE_NAM = 500;
	public static int CHOOSE_QUI = 501;
	public static int CHOOSE_THANG = 502;
	public static int CHOOSE_TUAN = 503;
	public static int CHOOSE_NGAY = 506;

	private static int TYPE_STRING = 300;
	private static int TYPE_INT = 301;

	public static int CHOOSE_CNTHU = 504;
	public static int CHOOSE_CNTRA = 505;

	private String strNhomChon = "";
	private String strKhoChon = "";
	private String strmMauChon = "";
	private String strmDetailMauChon = "";
	public static String user;

	private String kho = "", nhomHang = "", dbName = "", mau = "",
			detailMau = "";

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == REQUESTCODE_CHONKHO) {
			// SharedPreferences shareFr;
			//
			// shareFr = this.getSharedPreferences("valueDefault",
			// MODE_WORLD_WRITEABLE);
			// if(shareFr != null){
			// kho = shareFr.getString("kho", "");
			// mau = shareFr.getInt("mau", -1);
			// nhomHang = shareFr.getString("nhomHang", "");
			// }
			// get User
			if (resultCode == RESULT_OK) {
				strKhoChon = data.getStringExtra("strListKho");
				etChonKho.setText(data.getStringExtra("strListKho").toString());
			} else if (resultCode == RESULT_CANCELED) {
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tk_chonmau);

		user = super.getIntent().getExtras().getString("user");
		// declare component
		spMau = (Spinner) findViewById(R.id.spMau3);
		spNhom = (Spinner) findViewById(R.id.spNhom);
		etChonKho = (EditText) findViewById(R.id.etChonKho);
		dpNgay = (DatePicker) findViewById(R.id.dpNgay);
		btXem = (Button) findViewById(R.id.btXem);
		llNgay = (LinearLayout) findViewById(R.id.llChonNgay);
		llNhom = (LinearLayout) findViewById(R.id.llChonNhom);
		llKho = (LinearLayout) findViewById(R.id.llChonKho);
		spMauDetail = (Spinner) findViewById(R.id.spMauDetail);
		textViewDetail = (TextView) findViewById(R.id.textViewDetail);

		String dbNameToRef = "";
		// get database name edit by trangbm date 04/04/2013
		DBSystem dbcv = new DBSystem(this);
		dbcv.open();
		Cursor cur = dbcv.SelectSystemAll();
		while (cur.moveToNext()) {
			dbName = cur.getString(5);// get database name edit by trangbm date
										// 04/04/2013
		}
		cur.close();
		dbcv.close();
		// get database Name to defferences
		SharedPreferences shareFrDbName = this.getSharedPreferences(
				"dbNameTKMau", MODE_WORLD_WRITEABLE);
		if (shareFrDbName != null)
			dbNameToRef = shareFrDbName.getString("dataName", "");// get value
																	// to
																	// shareFrDbName

		// get value default for Chon K
		SharedPreferences shareFrKho, shareFrMau, shareFrNhomHang;
		shareFrKho = this
				.getSharedPreferences("valueKho", MODE_WORLD_WRITEABLE);
		shareFrMau = this
				.getSharedPreferences("valueMau", MODE_WORLD_WRITEABLE);
		shareFrNhomHang = this.getSharedPreferences("valueNhomhang",
				MODE_WORLD_WRITEABLE);
		if (dbNameToRef.equals(dbName)) {
			if (shareFrKho != null)
				kho = shareFrKho.getString("kho", "");
			if (shareFrMau != null)
				mau = shareFrMau.getString("mau", "");
			if (shareFrNhomHang != null)
				nhomHang = shareFrNhomHang.getString("nhomHang", "");
			// if (shareFrDetailMau != null)
			// detailMau = shareFrDetailMau.getString("detailmau", "");
		}

		// Mau

		//
		if (!kho.equals("")) {
			etChonKho.setText(kho);
			strKhoChon = kho;
		} else {
			etChonKho.setText(TK_ChonKho.getKhoDefault(user));
			strKhoChon = kho;
		}

		btXem.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String aSpMau = strmMauChon;
				if (aSpMau.equals("TH")) {// tong hop
					setBundleForTongHop(CHOOSE_TONGHOP, REQUESTCODE_TONGHOP);
				} else if (aSpMau.equals("CTT")) {// chi tiet thu
					setBundleForThuChi(CHOOSE_THU, REQUESTCODE_CTTHU);
				} else if (aSpMau.equals("CTC")) {// chi tiet chi
					setBundleForThuChi(CHOOSE_CHI, REQUESTCODE_CTCHI);
				} else if (aSpMau.equals("CTN")) {// chi tiet nhap
					setBundleForShipReceipt(CHOOSE_NHAP, REQUESTCODE_CTNHAP);
				} else if (aSpMau.equals("CTX")) { // chi tiet xuat
					setBundleForShipReceipt(CHOOSE_XUAT, REQUESTCODE_CTXUAT);
				} else if (aSpMau.equals("TMH")) {
					// tk tong mat hang theo Nam
					if (strmDetailMauChon.equals("NTMH"))
						setBundleForTongMatHang(CHOOSE_NAM,
								REQUESTCODE_TONGMATHANG);
					// tk tong mat hang theo Qui
					else if (strmDetailMauChon.equals("QTMH"))
						setBundleForTongMatHang(CHOOSE_QUI,
								REQUESTCODE_TONGMATHANG);
					// tk tong mat hang theo Thang
					else if (strmDetailMauChon.equals("TTMH"))
						setBundleForTongMatHang(CHOOSE_THANG,
								REQUESTCODE_TONGMATHANG);
					// tk tong mat hang theo tuan
					else if (strmDetailMauChon.equals("TUTMH"))
						setBundleForTongMatHang(CHOOSE_TUAN,
								REQUESTCODE_TONGMATHANG);
					// tk tong mat hang theo tuan
					else if (strmDetailMauChon.equals("NGTMH"))
						setBundleForTongMatHang(CHOOSE_NGAY,
								REQUESTCODE_TONGMATHANG);
				} else if (aSpMau.equals("CG")
						&& strmDetailMauChon.equals("DSCG")) {// danh sach cuoc
																// goi
					setBundleForDSCuocGoi(CHOOSE_DSCUOCGOI);
				}// cuoc goi chua giao
				else if (aSpMau.equals("CG")
						&& strmDetailMauChon.equals("CGIAO")) {
					setBundleForDSCuocGoi(CHOOSE_CHUAGIAO);
				} else if (aSpMau.equals("CG")
						&& strmDetailMauChon.equals("CTT")) {// cuoc goi chua
																// thanh toan
					setBundleForDSCuocGoi(CHOOSE_CHUATHANHTOAN);
				} else if (aSpMau.equals("CG")
						&& strmDetailMauChon.equals("CGCTT")) { // cuoc goi chua
																// giao - chua
																// thanh toan
					setBundleForDSCuocGoi(CHOOSE_CHUAGIAO_CHUATHANTOAN);
				} else if (aSpMau.equals("CG")
						&& strmDetailMauChon.equals("DB")) {// cuoc goi da ban
					setBundleForDSCuocGoi(CHOOSE_CUOCGOI_DABAN);
				} else if (aSpMau.equals("CG")
						&& strmDetailMauChon.equals("CB")) {// cuoc goi chua ban
					setBundleForDSCuocGoi(CHOOSE_CUOCGOI_CHUABAN);
				} else if (aSpMau.equals("XNNV")) {// xuat nuoc nhap vo
					ArrayList<String> col = new ArrayList<String>();
					col.add("ProductID");
					col.add("Xuat_N");
					col.add("Nhap_V");
					setBundleForNuocVo(CHOOSE_XNUOC_NVO, col);
				} else if (aSpMau.equals("XVNN")) {// xuat vo nhap nuoc
					ArrayList<String> col = new ArrayList<String>();
					col.add("ProductID");
					col.add("Xuat_V");
					col.add("Nhap_N");
					setBundleForNuocVo(CHOOSE_XVO_NNUOC, col);
				} else if (aSpMau.equals("TNTV")) {// ton nuoc ton vo
					ArrayList<String> col = new ArrayList<String>();
					col.add("ProductID");
					col.add("Ton_N");
					col.add("Ton_V");
					setBundleForNuocVo(CHOOSE_TONNV, col);
				} else if (aSpMau.equals("TKCG")
						&& strmDetailMauChon.equals("CGTK")) {// thong ke cuoc
																// goi theo kho
					setBundleTKCuocGoi(CHOOSE_TKCUOCGOI);
				} else if (aSpMau.equals("TKCG")
						&& strmDetailMauChon.equals("CGKTK")) {// thong ke cuoc
																// goi trong
																// ngay
					setBundleTKCuocGoi(CHOOSE_TKCUOCGOIBYKHO);
				} else if (aSpMau.equals("DSNV")) {// Doanh so nhan vien
					if (strmDetailMauChon.equals("NDSNV"))
						setBundleDoanhSoByNV(CHOOSE_NAM);
					else if (strmDetailMauChon.equals("QDSNV"))
						setBundleDoanhSoByNV(CHOOSE_QUI);
					else if (strmDetailMauChon.equals("TDSNV"))
						setBundleDoanhSoByNV(CHOOSE_THANG);
					else if (strmDetailMauChon.equals("TUDSNV"))
						setBundleDoanhSoByNV(CHOOSE_TUAN);
					else if (strmDetailMauChon.equals("NGDSNV"))
						setBundleDoanhSoByNV(CHOOSE_NGAY);
				} else if (aSpMau.equals("TLN")) { // Tong loi nhuan
					if (strmDetailMauChon.equals("NTLN"))
						setBundleTongLoiNhuan(CHOOSE_NAM);
					else if (strmDetailMauChon.equals("QTLN"))
						setBundleTongLoiNhuan(CHOOSE_QUI);
					else if (strmDetailMauChon.equals("TTLN"))
						setBundleTongLoiNhuan(CHOOSE_THANG);
					else if (strmDetailMauChon.equals("TUTLN"))
						setBundleTongLoiNhuan(CHOOSE_TUAN);
					else if (strmDetailMauChon.equals("NGTLN"))
						setBundleTongLoiNhuan(CHOOSE_NGAY);
				}// end doanh so nhan vien
				else if (aSpMau.equals("TKXT")) {// Thong ke xuat - ton
					setBundleTKXuatTon();
				}// end tk xuat - ton
				else if (aSpMau.equals("CN")) {// Cong no\
					// cong no phau thu
					if (strmDetailMauChon.equals("CNPT"))
						setBundleCongNo(CHOOSE_CNTHU);
					// cong no phai tra
					else if (strmDetailMauChon.equals("CNPTR"))
						setBundleCongNo(CHOOSE_CNTRA);
				}
				// clear value default
				clearValueDefault(aSpMau);
				// save value default
				saveValueDefault(aSpMau);
			}
		});

		// set nhomhang
		pCategorys = getProductCategogy();
		List<String> listNhom = new ArrayList<String>();
		listNhom.add("Tất cả");
		for (obProductCategory ob : pCategorys) {
			listNhom.add(ob.getTenLoai());
		}
		ArrayAdapter<String> adapterLoaiHang = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, listNhom);
		spNhom.setAdapter(adapterLoaiHang);
		// edit by trangbm date 02/04/2014
		if (!nhomHang.equals("")
				&& (Integer.valueOf(nhomHang) <= listNhom.size()))
			spNhom.setSelection(Integer.valueOf(nhomHang) );
		
		spNhom.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(!spNhom.getItemAtPosition(spNhom.getSelectedItemPosition()).equals("Tất cả"))
					strNhomChon = pCategorys.get(arg2-1).getMaLoai();
				else
					strNhomChon ="";
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});

//		if (!nhomHang.equals(""))
//			spNhom.setSelection(Integer.valueOf(nhomHang));

		etChonKho.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(TK_ChonMau.this,
						TK_ChonKho.class), REQUESTCODE_CHONKHO);
			}
		});

		// sp mau
		maus = getMaus();
		List<String> listMau = new ArrayList<String>();
		for (obMau ob : maus) {
			listMau.add(ob.getTenMau());
		}
		ArrayAdapter<String> adapterMau = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, listMau);
		spMau.setAdapter(adapterMau);
		// set value for chon mau
		if (!mau.equals(""))
			spMau.setSelection(Integer.valueOf(mau));
		spMau.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				strmMauChon = maus.get(arg2).getMaMau().trim();
				setVisible(strmMauChon);
				// detail Mau date 15/04/2014
				detailMaus = getDetailMaus(strmMauChon.trim());
				if (detailMaus != null && detailMaus.size() != 0) {
					spMauDetail.setVisibility(View.VISIBLE);
					textViewDetail.setVisibility(View.VISIBLE);
					List<String> listDetailMau = new ArrayList<String>();
					for (obDetailMau obDetail : detailMaus) {
						listDetailMau.add(obDetail.getTenDetialMau());
					}
					ArrayAdapter<String> adapterDetailMau = new ArrayAdapter<String>(
							TK_ChonMau.this,
							android.R.layout.simple_spinner_dropdown_item,
							listDetailMau);
					spMauDetail.setAdapter(adapterDetailMau);
					SharedPreferences shareFrDetailMau;
					shareFrDetailMau = TK_ChonMau.this.getSharedPreferences(
							"valueDetailMau", MODE_WORLD_WRITEABLE);
					if (shareFrDetailMau != null) {
						if (strmMauChon.equals("TMH")) {
							detailMau = shareFrDetailMau.getString(
									"detailmauTMH", "");
						} else if (strmMauChon.equals("CG")) {
							detailMau = shareFrDetailMau.getString(
									"detailmauCG", "");
						} else if (strmMauChon.equals("TKCG")) {
							detailMau = shareFrDetailMau.getString(
									"detailmauTKCG", "");
						} else if (strmMauChon.equals("DSNV")) {// Doanh so nhan
																// vien
							detailMau = shareFrDetailMau.getString(
									"detailmauDSNV", "");
						} else if (strmMauChon.equals("TLN")) { // Tong loi
																// nhuan
							detailMau = shareFrDetailMau.getString(
									"detailmauTLN", "");
						} else if (strmMauChon.equals("CN")) {// Cong no
							detailMau = shareFrDetailMau.getString(
									"detailmauCN", "");
						}
						if (!detailMau.equals("") && (Integer.valueOf(detailMau) <= listDetailMau.size()))
							spMauDetail.setSelection(Integer.valueOf(detailMau));
					}
					spMauDetail
							.setOnItemSelectedListener(new OnItemSelectedListener() {

								public void onItemSelected(AdapterView<?> arg0,
										View v, int index, long arg3) {
									// TODO Auto-generated method stub
									strmDetailMauChon = detailMaus.get(index)
											.getMaDetailMau().trim();
									setVisible(strmMauChon);
								}

								public void onNothingSelected(
										AdapterView<?> arg0) {
									// TODO Auto-generated method stub

								}
							});
					// end detail Mau
				} else {
					spMauDetail.setVisibility(View.GONE);
					textViewDetail.setVisibility(View.GONE);
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}

		});
		// end sp mau
	}

	private ArrayList<obProductCategory> getProductCategogy() {
		ArrayList<obProductCategory> productCategory = new ArrayList<obProductCategory>();
		String sql = " SELECT DISTINCT LoaiHang.[Category ID] as MaLoai, LoaiHang as TenLoai "
				+ " FROM products, loaihang "
				+ " WHERE LoaiHang.[Category ID] = Products.[Category ID] ";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			while (rs.next()) {
				obProductCategory ojPCategory = new obProductCategory();
				if (rs.getString("MaLoai") != null)
					ojPCategory.setMaLoai(rs.getString("MaLoai").toString());
				if (rs.getString("TenLoai") != null)
					ojPCategory.setTenLoai(rs.getString("TenLoai").toString());
				productCategory.add(ojPCategory);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return productCategory;
	}

	private void setBundleForDSCuocGoi(int value) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);
		// put value for DSCuocGoi
		Intent iDate = new Intent(TK_ChonMau.this, DSCuocGoi.class);
		Bundle bDate = new Bundle();
		bDate.putIntegerArrayList("date", date);
		bDate.putInt("choose_mau", value);
		bDate.putString("strKho", strKhoChon);
		iDate.putExtras(bDate);
		startActivityForResult(iDate, REQUESTCODE_CUOCGOI);
	}

	private void setBundleForShipReceipt(int choose, int requestCode) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);

		Intent i = new Intent(TK_ChonMau.this, TK_NhapXuat.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_actual", date);
		b.putInt("choose_mau", choose);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		i.putExtras(b);
		startActivityForResult(i, requestCode);
	}

	private void setBundleForThuChi(int choose, int requestCode) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);

		Intent i = new Intent(TK_ChonMau.this, TK_ThuChi.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_actual", date);
		b.putInt("choose_mau", choose);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		i.putExtras(b);
		startActivityForResult(i, requestCode);
	}

	private void setBundleForTongHop(int choose, int requestCode) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);

		Intent i = new Intent(TK_ChonMau.this, TK_TongHop.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_actual", date);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		i.putExtras(b);
		startActivityForResult(i, requestCode);
	}

	private void setBundleForNuocVo(int iChooseNuocvo,
			ArrayList<String> colNames) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth() + 1;
		int iYear = dpNgay.getYear();

		String strDate = String.valueOf(iYear) + formatNumberAdd0(iMonth)
				+ formatNumberAdd0(iDay);

		Intent i = new Intent(TK_ChonMau.this, TK_NuocVo.class);
		Bundle b = new Bundle();
		b.putInt("iChooseNuocVo", iChooseNuocvo);
		b.putString("strDate", strDate);
		b.putStringArrayList("colNames", colNames);
		b.putString("strKhos", etChonKho.getText().toString());
		i.putExtras(b);
		startActivityForResult(i, REQUESTCODE_NUOCVO);
	}

	private String formatNumberAdd0(int iNumber) {
		try {
			if (iNumber < 10)
				return "0" + String.valueOf(iNumber);
			else
				return String.valueOf(iNumber);
		} catch (Exception e) {
			return String.valueOf(iNumber);
		}
	}

	private void setBundleForTongMatHang(int choose, int requestCode) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);

		Intent i = new Intent(TK_ChonMau.this, TK_TongMatHang.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_actual", date);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		b.putInt("choose_tmh", choose);
		i.putExtras(b);
		startActivityForResult(i, requestCode);
	}

	private void setEnableParameter(Boolean isShowNgay, Boolean isShowNhom,
			Boolean isShowKho) {
		if (isShowNgay)
			llNgay.setVisibility(View.VISIBLE);
		else
			llNgay.setVisibility(View.GONE);
		if (isShowNhom)
			llNhom.setVisibility(View.VISIBLE);
		else
			llNhom.setVisibility(View.GONE);
		if (isShowKho)
			llKho.setVisibility(View.VISIBLE);
		else
			llKho.setVisibility(View.GONE);
	
	}

	private void setBundleTKCuocGoi(int choose) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);
		// put value for DSCuocGoi
		Intent iDate = new Intent(TK_ChonMau.this, TK_CuocGoi.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_tk_cuocgoi", date);
		b.putInt("chooseType", choose);
		b.putString("strKho", strKhoChon);
		// b.putInt("choosemau_tk_cuocgoi", choose);
		iDate.putExtras(b);
		startActivityForResult(iDate, REQUESTCODE_TKCUOCGOI);
	}

	public void saveValueDefaultByString(int type, String name, String value,
			String nameIndex) {
		SharedPreferences sharePre = getSharedPreferences(nameIndex,
				MODE_WORLD_READABLE);
		SharedPreferences.Editor prefEditor = sharePre.edit();
		if (type == TYPE_STRING)
			prefEditor.putString(name, value);
		else
			prefEditor.putInt("mau", Integer.valueOf(value));
		prefEditor.commit();
	}

	/**
	 * @author Administrator
	 * @param type
	 * @param name
	 * @param NameIndex
	 */
	public void clearValueDefault(int type, String name, String NameIndex) {
		SharedPreferences sharePre = getSharedPreferences(NameIndex,
				MODE_WORLD_READABLE);
		SharedPreferences.Editor prefEditor = sharePre.edit();
		if (type == TYPE_STRING)
			prefEditor.putString(name, "");
		else
			prefEditor.putInt(name, 0);
	}

	public void saveValueDefaultByString(String nameIndex, String nameValue,
			String value) {
		SharedPreferences sharePre = getSharedPreferences(nameIndex,
				MODE_WORLD_READABLE);
		SharedPreferences.Editor prefEditor = sharePre.edit();
		prefEditor.putString(nameValue, value);
		prefEditor.commit();
	}

	public void clearValueDefault(String nameIndex, int type, String nameValue) {
		SharedPreferences sharePre = getSharedPreferences(nameIndex,
				MODE_WORLD_READABLE);
		SharedPreferences.Editor prefEditor = sharePre.edit();
		if (type == 0)
			prefEditor.putString(nameValue, "");
		else
			prefEditor.putInt(nameValue, -1);
	}

	private void setBundleDoanhSoByNV(int choose) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);
		// put value for Doanh so by Nhan vien
		Intent i = new Intent(TK_ChonMau.this, TK_DanhSoByNhanVien.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_tk_DoanhsoNV", date);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		b.putInt("chose_DSNV", choose);
		i.putExtras(b);
		startActivityForResult(i, REQUESTCODE_TK_DSNV);
	}

	private ArrayList<obMau> getMaus() {
		ArrayList<obMau> obMaus = new ArrayList<obMau>();
		String sql = " SELECT * FROM mobileMau ";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			while (rs.next()) {
				obMau obMau = new obMau();
				if (rs.getString(1) != null)
					obMau.setMaMau(rs.getString(1).toString());
				if (rs.getString(2) != null)
					obMau.setTenMau(rs.getString(2).toString());
				obMaus.add(obMau);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obMaus;
	}

	private ArrayList<obDetailMau> getDetailMaus(String maMau) {
		ArrayList<obDetailMau> obDetailMaus = new ArrayList<obDetailMau>();
		String sql = " SELECT * FROM mobileDetailMau WHERE MaMau = '" + maMau
				+ "'";
		ResultSet rs = Global.excuteQuerySQLServer(sql);
		try {
			while (rs.next()) {
				obDetailMau obDMau = new obDetailMau();
				if (rs.getString(1) != null)
					obDMau.setMaDetailMau(rs.getString(1).toString());
				if (rs.getString(2) != null)
					obDMau.setTenDetialMau(rs.getString(2).toString());
				obDetailMaus.add(obDMau);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obDetailMaus;
	}

	private void setBundleTongLoiNhuan(int choose) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);
		// put value for Doanh so by Nhan vien
		Intent i = new Intent(TK_ChonMau.this, TK_TongLoiNhuan.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_tk_tln", date);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		b.putInt("chose_tln", choose);
		i.putExtras(b);
		startActivityForResult(i, REQUESTCODE_TK_DSNV);
	}

	private void setBundleTKXuatTon() {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);
		// put value for Doanh so by Nhan vien
		Intent i = new Intent(TK_ChonMau.this, TK_XuatVaTon.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_tk_xuatton", date);
		b.putString("strNhomChon", strNhomChon);
		b.putString("strKhoChon", strKhoChon);
		// b.putInt("chose_tln", choose);
		i.putExtras(b);
		startActivityForResult(i, REQUESTCODE_TK_XUATTON);
	}

	private void setBundleCongNo(int choose) {
		int iDay = dpNgay.getDayOfMonth();
		int iMonth = dpNgay.getMonth();
		int iYear = dpNgay.getYear();

		ArrayList<Integer> date = new ArrayList<Integer>();
		date.add(0, iDay);
		date.add(1, iMonth + 1);
		date.add(2, iYear);

		Intent i = new Intent(TK_ChonMau.this, CongNo.class);
		Bundle b = new Bundle();
		b.putIntegerArrayList("date_tk_cn", date);
		b.putString("strKhoChon", strKhoChon);
		b.putInt("chose_cn", choose);
		i.putExtras(b);
		startActivityForResult(i, REQUESTCODE_CONGNO);
	}

	public void clearValueDefault(String valueMau) {
		// clear value default
		clearValueDefault(TYPE_STRING, "kho", "valueKho");
		clearValueDefault(TYPE_STRING, "nhomHang", "valueNhomhang");
		clearValueDefault(TYPE_STRING, "mau", "valueMau");
		if (valueMau.equals("TMH")) {
			clearValueDefault(TYPE_STRING, "detailmauTMH", "valueDetailMau");
		} else if (valueMau.equals("CG")) {
			clearValueDefault(TYPE_STRING, "detailmauCG", "valueDetailMau");
		} else if (valueMau.equals("TKCG")) {
			clearValueDefault(TYPE_STRING, "detailmauTKCG", "valueDetailMau");
		} else if (valueMau.equals("DSNV")) {// Doanh so nhan vien
			clearValueDefault(TYPE_STRING, "detailmauDSNV", "valueDetailMau");
		} else if (valueMau.equals("TLN")) { // Tong loi nhuan
			clearValueDefault(TYPE_STRING, "detailmauTLN", "valueDetailMau");
		} else if (valueMau.equals("CN")) {// Cong no
			clearValueDefault(TYPE_STRING, "detailmauCN", "valueDetailMau");
		}
		clearValueDefault("dbNameTKMau", 0, dbName);
	}

	public void saveValueDefault(String valueMau) {
		// save value default
		saveValueDefaultByString(TYPE_STRING, "kho", etChonKho.getText()
				.toString(), "valueKho");
		saveValueDefaultByString(TYPE_STRING, "nhomHang",
				String.valueOf(spNhom.getSelectedItemPosition()),
				"valueNhomhang");
		saveValueDefaultByString(TYPE_STRING, "mau",
				String.valueOf(spMau.getSelectedItemPosition()), "valueMau");

		if (valueMau.equals("TMH")) {
			saveValueDefaultByString(TYPE_STRING, "detailmauTMH",
					String.valueOf(spMauDetail.getSelectedItemPosition()),
					"valueDetailMau");
		} else if (valueMau.equals("CG")) {
			saveValueDefaultByString(TYPE_STRING, "detailmauCG",
					String.valueOf(spMauDetail.getSelectedItemPosition()),
					"valueDetailMau");
		} else if (valueMau.equals("TKCG")) {
			saveValueDefaultByString(TYPE_STRING, "detailmauTKCG",
					String.valueOf(spMauDetail.getSelectedItemPosition()),
					"valueDetailMau");
		} else if (valueMau.equals("DSNV")) {// Doanh so nhan vien
			saveValueDefaultByString(TYPE_STRING, "detailmauDSNV",
					String.valueOf(spMauDetail.getSelectedItemPosition()),
					"valueDetailMau");
		} else if (valueMau.equals("TLN")) { // Tong loi nhuan
			saveValueDefaultByString(TYPE_STRING, "detailmauTLN",
					String.valueOf(spMauDetail.getSelectedItemPosition()),
					"valueDetailMau");
		} else if (valueMau.equals("CN")) {// Cong no
			saveValueDefaultByString(TYPE_STRING, "detailmauCN",
					String.valueOf(spMauDetail.getSelectedItemPosition()),
					"valueDetailMau");
		}
		saveValueDefaultByString("dbNameTKMau", "dataName", dbName);
	}

	public void setVisible(String valueMau) {
		if (valueMau.equals("TH") || valueMau.equals("CTT")
				|| valueMau.equals("CTC") || valueMau.equals("CG")
				|| valueMau.equals("XNNV") || valueMau.equals("XVNN")
				|| valueMau.equals("TNTV") || valueMau.equals("CN")
				||  (valueMau.equals("TKCG") && strmDetailMauChon.equals("CGTK"))) {// chi
																		// tiet
																		// chi
			setEnableParameter(true, false, true);
		} else if (valueMau.equals("CTN")
				|| valueMau.equals("CTX")
				|| valueMau.equals("TMH")
				|| valueMau.equals("TLN")
				|| valueMau.equals("TKXT")
				|| (valueMau.equals("DSNV") )) {// Doanh so nhan vien
			setEnableParameter(true, true, true);
		} else if (valueMau.equals("TKCG") && strmDetailMauChon.equals("CGKTK")) {
			setEnableParameter(true, false, false);
		}
	}
}
